package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhentao.mapper.CartInfoMapper;
import com.zhentao.mapper.ProductSkuMapper;
import com.zhentao.model.entity.h5.CartInfo;
import com.zhentao.model.entity.product.ProductSku;
import com.zhentao.service.CartInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartInfoServiceImpl implements CartInfoService {
    @Autowired
    private CartInfoMapper cartInfoMapper;
    @Autowired
    private ProductSkuMapper productSkuMapper;

    @Override
    public List< CartInfo > findAllById( Long id ) {
        List< CartInfo > list = cartInfoMapper.selectList( Wrappers.< CartInfo > lambdaQuery().eq( CartInfo::getUserId, id ) );
        for (CartInfo cartInfo : list) {
            if (cartInfo != null) {
                ProductSku productSku = productSkuMapper.selectById( cartInfo.getSkuId() );
                cartInfo.setSkuName( productSku.getSkuName() );
                cartInfo.setImgUrl( productSku.getThumbImg() );
            }

        }
        return list;
    }

    @Override
    public void add( CartInfo cartInfo ) {
        cartInfoMapper.insert( cartInfo );
    }
}
